/**
 * laserTraq configurator - Daniel Hiepler - (c) 2007-2013
 *
 * @defgroup main main
 * @author D.Hiepler <daniel-laser@boogiepalace.hopto.org>
 * @{
 */
 

#include "config.h"
#include "splash.h"
#include "main.h"
#include "gui.h"
#include "prefs.h"
#include "output.h"
#include "source.h"
#include "gstreamer.h"
#include "sensor.h"


/**
 * main()
 * @param argc - argument count
 * @param argv - argument array
 */
int main(int argc, char *argv[])
{
    int error;
    char tmpstring[255];
    GtkStyle *style;
    GtkWidget *splash, *splash_image;
    GdkBitmap *mask;
    GdkPixmap *splash_map;
    
    error = 0;
    prefs_set_verbose(1);
    prefs_set_quiet(0);
       
    
    /*
     * init gtk 
     */
    gtk_init(&argc, &argv);

    /*
     * init glade 
     */
    glade_init();

    
    /*
     * early gtk-init for splash-screen 
     */
#ifdef ENABLE_NLS
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
    textdomain(PACKAGE);
#endif

    
    /*
     * build filename for splash-image 
     */
    snprintf((char *) &tmpstring, 254, "%s/%s/icons/splash.png",
             DATADIR,PACKAGE);

		
    /*
     * build up splash-screen 
     */
    splash = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(splash), GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_keep_above(GTK_WINDOW(splash), TRUE);
    gtk_window_set_decorated(GTK_WINDOW(splash), FALSE);

    /*
     * load image 
     */
    if((splash_image = gtk_image_new_from_file((gchar *) & tmpstring))) {

                
        /*
         * add image to window 
         */
        gtk_container_add(GTK_CONTAINER(splash), splash_image);
        
        /*
         * show 
         */
        gtk_widget_queue_draw(splash_image);
        gtk_widget_show_all(splash);
        
        while (gtk_events_pending()) {
            gtk_main_iteration();
        }
        
        /*
         * add transparency 
         */
        style = gtk_widget_get_default_style();
        if(!(splash_map = gdk_pixmap_create_from_xpm_d(splash->window, &mask,
            &style->bg[GTK_STATE_NORMAL], splash_shape_xpm)))
                    goto m_error;
                
        gtk_widget_shape_combine_mask(splash, mask, 0, 0);

        /*
         * show 
         */
        gtk_widget_queue_draw(splash_image);
        gtk_widget_show_all(splash);
        
        while (gtk_events_pending()) {
            gtk_main_iteration();
        }

    }
    else {
        printf("ERROR: Failed to load splash-screen \"%s\"\n",
               (char *) &tmpstring);
    }

    /*
     * init gstreamer 
     */
    gst_init(&argc, &argv);
    
    
    /* init sensor-module */
    if(sensor_init() < 0)
    {
        _VERBOSE1 printf("Failed to initialize sensor-GUI...\n");
        goto m_error;
    }
    
    /* init output-module */
    if(output_init() < 0)
    {
        _VERBOSE1 printf("Failed to initialize output-GUI...\n");
        goto m_error;
    }        
    
    /* init video-input */
    if(src_init() < 0)
    {
        _VERBOSE1 printf("Failed to initialize video-input...\n");
        goto m_error;
    }
    
    
    /* initialize preferences */
    if(prefs_init(argc, argv) < 0)
    {
        printf("prefs_init() failed...\n");
        error = -1;
        goto m_error;
    }
    
    /*
     * init gtk stuff 
     */
    if(gui_init() < 0)
    {
        _VERBOSE1 printf("Failed to initialize main GUI...\n");
        goto m_error;
    }
  
   
    /*
     * start capture on current source 
     */
    if(src_get())
        gstr_capture_start(src_get());

    /*
     * destroy splash image 
     */
    gtk_widget_destroy(splash);

    /*
     * main-loop 
     */
    gtk_main();


    

  m_error:
    gui_cleanup();
    src_cleanup();
    prefs_cleanup();
    


    return error;
}

/** 
 * @} 
 */
